from strategy.core.Stg import Stg
import talib.abstract as ta
import math
import pandas as pd


class UpLimit(Stg):
    """
    涨停板战法: 涨停买入, 涨停结束卖出
    """

    upLimitStatus = 0

    def initial(self):
        return

    def getParams(self) -> list[dict]:
        return [
            {'keepBy': "upLimit"},
            {'keepBy': "up"}
        ]

    def signal(self, index):
        priceDetial = self.getPriceDetail()

        # 涨停买入
        if self.upLimitStatus == 0 and priceDetial['pct_chg'] > 9.9:
            self.upLimitStatus = 1
            return 1
        if self.param['keepBy'] == "upLimit":
            # 涨停结束卖出
            if self.upLimitStatus == 1 and priceDetial['pct_chg'] < 9.9:
                self.upLimitStatus = 0
                return -1
        else:
            # 涨结束卖出
            if self.upLimitStatus == 1 and priceDetial['pct_chg'] < 0:
                self.upLimitStatus = 0
                return -1
        return 0
